$(document).ready(function() {
	$("form[name='newPurchaseItemForm']").submit(addPurchaseItem);		
	//midifyDialog初始化
	$(".modifyDialog").dialog({
		autoOpen : false,
		modal : true,
		minHeight : 400,
		minWidth : 350
	});
	//表格最上头的全选checkbox
	$("#purchaseItems :checkbox[name='checkAll']").click(function() {
		var checked = $(this).attr("checked");
		var checkboxes = $("#purchaseItems :checkbox:enabled[name='item']");
		if (checked) {
			checkboxes.attr("checked", "checked");
		} else {
			checkboxes.removeAttr("checked");
		}
	});
	//删除按钮
	$(":button[name='delete']").click(handleDelete);
	//查看修改按钮
	$("#purchaseItems :button[name='modify']").click(handleModify);
	//filterForm处理checkbox
	$(".filterForm :checkbox[name='applied']").click(function() {
		var checkBoxOfProcessed = $(".filterForm :checkbox[name='processed']");
		var checked = $(this).attr("checked");
		if (checked) {
			$(this).attr("checked", "checked");
			checkBoxOfProcessed.attr("checked", "checked").attr("disabled", "disabled");
		} else {
			$(this).removeAttr("checked");
			checkBoxOfProcessed.removeAttr("disabled");
		}
	});
});

function addPurchaseItem() {	
	var newPurchaseItemForm = $(this);
	var title = $(this).find(":text[name='title']").val();
	var description = $(this).find("textarea[name='description']").val();
	var contactTel = $(this).find(":text[name='contactTel']").val();
	if (title.length == 0 || description.length == 0 || contactTel.length == 0) {
		alert("必填信息必须填写");
		return false;
	}
	
	var purchaseItem = {
		"title" : title,
		"description" : description,
		"contactTel" : contactTel
	};		
	
	$.ajax({
		type : "POST",
		data : purchaseItem,
		dataType : "json",
		cache : false,
		async : false,
		url : "addPurchaseItem.do",
		success : function(json, textStatus) {
			if (json.status) {
				var messageDialog = $(".messageDialog");
				messageDialog.text(json.message);
				messageDialog.dialog("destroy");
				messageDialog.dialog({
					"autoOpen" : true,
					"modal" : true,
					"buttons" : {
						"查看购置单列表" : function() {
							window.location = "purchaseItemList.do";
						},
						"继续添加" : function() {
							newPurchaseItemForm.find(":text[name='title']").val("");
							newPurchaseItemForm.find("textarea[name='description']").val("");
							$(this).dialog("close");
						}
					}
				});
			} else {
				$(".ajaxMessage").text("请求失败，请稍后再试...").show();
			}
		},
		error : function() {
			$(".ajaxMessage").text("请求失败，系统无响应...").show();
		}
	});
	return false;
}

function handleDelete() {
	var purchaseItemIds = new Array();
	$("#purchaseItems :checkbox:checked[name='item']").each(function() {
		purchaseItemIds.push($(this).val());
	});
	
	if (purchaseItemIds.length == 0) {
		return false;
	}
	var confirmed = confirm("你确定要删除吗?");
	if (confirmed) {
		deletePurchaseItem(purchaseItemIds);
	} 
	
	return false;
}

function deletePurchaseItem(purchaseItemIds) {
	$.ajax({
		async : false,
		type : "GET",
		dataType : "json",
		data : {
			"purchaseItemId" : purchaseItemIds
		},
		url : "deletePurchaseItem.do",
		success : function(json, textStatus) {
			if (json.status) {
				$(".ajaxMessage").text(json.message).show();
				setTimeout(function() {location.reload(true)}, 1000);
			} else {
				$(".ajaxMessage").text(json.message).show();
			}
		},
		errors : function(XMLHttpRequest, textStatus, errorThrown) {
			$(".ajaxMessage").text("删除失败，请稍后再试...").show();
		}
	});
}

function handleModify() {
	var tr = $(this).parents("tr");
	var purchaseItemId = tr.find("td[title='id']").text();
	var title = tr.find("td[title='title']").text();
	var description = tr.find("td[title='description']").text();
	var contactTel = tr.find("td[title='contactTel']").text();
	var updatable = !(tr.find(":checkbox[name='processed']").attr("checked"));
	var modifyDialog = $(".modifyDialog");
	modifyDialog.find(":hidden[name='purchaseItemId']").val(purchaseItemId);
	modifyDialog.find(":text[name='title']").val(title);
	modifyDialog.find("textarea[name='description']").val(description);
	modifyDialog.find(":text[name='contactTel']").val(contactTel);
	modifyDialog.dialog("open");
	
	if (updatable) {
		modifyDialog.dialog("option", "buttons", {
			"取消" : function() {
				$(this).dialog("close");
			},
			"更新" : function() {
				updatePurchaseItem(tr);
			}
		});
	} else {
		modifyDialog.dialog("option", "buttons", {
			"取消" : function() {
				$(this).dialog("close");
			}
		});
	}
}

function updatePurchaseItem(tr) {
	var modifyDialog = $(".modifyDialog");
	var purchaseItemId = modifyDialog.find(":hidden[name='purchaseItemId']").val();
	var title = modifyDialog.find(":text[name='title']").val();
	var description = modifyDialog.find("textarea[name='description']").val();
	var contactTel = modifyDialog.find(":text[name='contactTel']").val();
	var purchaseItem = {
		"id" : purchaseItemId,
		"title" : title,
		"description" : description,
		"contactTel" : contactTel
	};
	
	if (purchaseItem.title.length == 0 
			|| purchaseItem.description.length == 0
			|| purchaseItem.contactTel.length == 0) {
		alert("必填信息不能留空!");
		return;
	}
	$.ajax({
		async : true,
		type : "POST",
		data : purchaseItem,
		dataType : "json",
		url : "updatePurchaseItem.do",
		success : function (json, textStatus) {
			if (json.status) {
				tr.find("td[title='title']").text(purchaseItem.title);
				tr.find("td[title='description']").text(purchaseItem.description);
				tr.find("td[title='contactTel']").text(purchaseItem.contactTel);				
			}
			$(".ajaxMessage").text(json.message).show();
		},
		error : function (XMLHttpRequest, textStatus, errorThrown) {
			$(".ajaxMessage").text("更新失败，请稍后再试...").show();
		}
	});
	$(".modifyDialog").dialog("close");
}